import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/widges/appbar.dart';
import 'package:guochaoapp/controller/shopping/order_detail_controller.dart';
import 'package:guochaoapp/controller/shopping/order_invoice_controller.dart';

import '../../r.dart';

class GCOrderInvoicePage extends StatelessWidget {
  GCOrderInvoiceController c;
  GCOrderDetailController detailC;
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  bool hasInvoiced;

  @override
  Widget build(BuildContext context) {
    c = Get.put(GCOrderInvoiceController());
    hasInvoiced = Get.arguments;
    print(hasInvoiced);
    detailC = Get.find();
    c.requestInvoiceData();
    return Scaffold(
        appBar: GCAppBar(
          titleText: hasInvoiced ? "查看发票" : "申请开票",
          backTap: () {
            Get.back();
            detailC.requestInitData(detailC.orderDetail.value.mallOrderNo);
          },
        ),
        body: Obx(() => _initSubviews()));
  }

  _initSubviews() {
    return Column(
      children: [
        _getTopView(),
        _getInvoiceView(),
        // _getDescriptionView(),
        Expanded(child: SizedBox()),
        // SizedBox(height: 27),
        _getButtonView(),
      ],
    );
  }

  _getTopView() {
    return Container(
      height: 82,
      color: Colors.white,
      padding: EdgeInsets.only(left: 13),
      child: Column(
        children: [
          SizedBox(height: 13),
          Row(
            children: [Text("订单号 :  ${detailC.orderDetail.value.mallOrderNo}")],
          ),
          SizedBox(height: 13),
          Row(
            children: [
              Text("开票金额 : "),
              Text(
                  "￥${detailC.orderDetail.value.paymentPriceTotal - detailC.orderDetail.value.returnPriceTotal}",
                  style: TextStyle(color: Colors.red, fontSize: 20))
            ],
          )
        ],
      ),
    );
  }

  _getInvoiceView() {
    return Container(
      height: 450,
      color: Colors.white,
      margin: EdgeInsets.only(left: 8, right: 8, top: 4),
      padding: EdgeInsets.only(left: 24, right: 24),
      child: ListView(
        children: [
          Form(
            key: _formKey,
            child: ListView.builder(
                itemCount: c.textEditingControllers.length,
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (context, index) {
                  return _itemView(index);
                }),
          )
        ],
      ),
    );
  }

  _getButtonView() {
    if (hasInvoiced) {
      return Container(
          color: Colors.white,
          width: ScreenUtil().setSp(359),
          height: ScreenUtil().setSp(30),
          margin: EdgeInsets.only(
              bottom: ScreenUtil().setSp(10),
              left: ScreenUtil().setSp(8),
              right: ScreenUtil().setSp(8)),
          child: Center(
            child: Text("纸质发票预计在申请开票后的2-5个工作日内寄出", style: TextStyle(color: Colors.red)),
          )
      );
    } else {
      return Container(
        margin: EdgeInsets.only(
            bottom: ScreenUtil().setSp(10),
            left: ScreenUtil().setSp(8),
            right: ScreenUtil().setSp(8)),
        child: Row(
          children: [
            Container(
              width: ScreenUtil().setSp(172),
              height: ScreenUtil().setSp(30),
              child: OutlineButton(
                child: Text("取消"),
                padding: EdgeInsets.zero,
                color: Colors.white,
                textColor: Colors.red,
                borderSide: BorderSide(color: Colors.red),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                onPressed: () {
                  Get.back();
                },
              ),
            ),
            SizedBox(width: ScreenUtil().setSp(15)),
            Container(
              width: ScreenUtil().setSp(172),
              height: ScreenUtil().setSp(30),
              child: FlatButton(
                child: Text("提交申请"),
                padding: EdgeInsets.zero,
                color: Color(0xFF40A8B3),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                onPressed: () {
                  c.submitInvoice(detailC.orderDetail.value.mallOrderNo);
                },
              ),
            ),
          ],
        ),
      );
    }
  }

  _itemView(int index) {
    return Container(
      height: 50,
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            children: [
              Expanded(
                flex: 10,
                child: Text(
                  c.titles[index],
                  style: TextStyle(color: Color(0xFF333333), fontSize: 16),
                ),
              ),
              Expanded(
                flex: 20,
                child: TextFormField(
                  enabled: !hasInvoiced,
                  controller: c.textEditingControllers[index],
                  textInputAction: TextInputAction.next,
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.only(left: 10),
                      hintText: "必填",
                      hintStyle:
                          TextStyle(fontSize: 13, color: Color(0xFFD8D8D8)),
                      enabledBorder: InputBorder.none,
                      disabledBorder: InputBorder.none,
                      focusedBorder: InputBorder.none),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
